//
//  HZMainViewController.m
//  MyDoctor
//
//  Created by 黑眼圈 on 16/5/4.
//  Copyright © 2016年 heiyan. All rights reserved.
//主控制器

#import "HZMainViewController.h"



//define this constant if you want to use Masonry without the 'mas_' prefix
#define MAS_SHORTHAND
//define this constant if you want to enable auto-boxing for default syntax
#define MAS_SHORTHAND_GLOBALS


#import "Masonry.h"

@interface HZMainViewController ()

@property(nonatomic,strong)HZWeathrView*  weatherView;
@property(nonatomic,strong)HZCycleView*  cycleView;
@property(nonatomic,strong)HZDiseaseCollView*  diseaseView;

@end

@implementation HZMainViewController

//判断是否登录
bool is = YES;

- (void)viewDidLoad {
    [super viewDidLoad];

    
    //设置并添加子控件
    [self setupUI];
    
    //设置弹窗侧边抽屉的按钮
    [self setupDrawer];
    
    //接收通知
    [self receiveNoti];
    
}


#pragma mark -
#pragma mark     接收通知
-(void)receiveNoti{
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didDisease:) name:@"didDisease" object:nil];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(cityLabelClick) name:@"cityLabelClick" object:nil];
}



#pragma mark -
#pragma mark     实现通知方法
-(void)didDisease:(NSNotification*)noti{
    
//    NSLog(@"%@",noti.object);
    
    
    //如果未登录
        if (!is) {
    
    //未登录提示弹窗
    [HZAlertView showErrorAlert];
        } else {
    
            
            
            [self gotoDisease:noti.object];
        
        }

    
}

/// 点击了天气视图里面的城市按钮
-(void)cityLabelClick{
    
}


#pragma mark -
#pragma mark     跳转病情控制器
-(void)gotoDisease:(NSString*)text{
    
    
    
    HZMainViewController *mainVC = [[HZMainViewController alloc]init];
    
    
    [self.mm_drawerController setCenterViewController:[[UINavigationController alloc]initWithRootViewController:mainVC] withCloseAnimation:YES completion:nil];
    
    
    
    if ([text isEqualToString:@"肿瘤"]) {
        
        //创建登录控制器
        HZTumourVc* tumourVc = [[HZTumourVc alloc]init];
        
        //跳转到登录控制器
        [mainVC.navigationController pushViewController:tumourVc animated:YES];
        

    }else if ([text isEqualToString:@"心血管"]){
        
        //创建登录控制器
        HZCardiovascularVc* cardiovascularVc = [[HZCardiovascularVc alloc]init];
        
        //跳转到登录控制器
        [mainVC.navigationController pushViewController:cardiovascularVc animated:YES];
        
        
    }else if ([text isEqualToString:@"神经科"]){
        
        //创建登录控制器
        HZNerveVc* nerveVc = [[HZNerveVc alloc]init];
        
        //跳转到登录控制器
        [mainVC.navigationController pushViewController:nerveVc animated:YES];
        
    }else if ([text isEqualToString:@"血液科"]){
        
        //创建登录控制器
        HZHematologyVc* hematologyVc = [[HZHematologyVc alloc]init];
        
        //跳转到登录控制器
        [mainVC.navigationController pushViewController:hematologyVc animated:YES];
        
    }else if ([text isEqualToString:@"骨科"]){
        
        //创建登录控制器
        HZOrthopedicsVc* orthopedicsVc = [[HZOrthopedicsVc alloc]init];
        
        //跳转到登录控制器
        [mainVC.navigationController pushViewController:orthopedicsVc animated:YES];
        
    }else{
        
        //创建登录控制器
        HZExerciseVc* exerciseVc = [[HZExerciseVc alloc]init];
        
        //跳转到登录控制器
        [mainVC.navigationController pushViewController:exerciseVc animated:YES];
        
    }
    
    
}

-(void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

#pragma mark -
#pragma mark     设置并添加子控件
-(void)setupUI{
    
    //添加背景色
    self.view.backgroundColor = [UIColor whiteColor];
    
    //设置导航栏中间的 title
    self.navigationItem.title = @"宝芝林";
    
    
    //添加子控件
    [self.view addSubview:self.weatherView];
    [self.view addSubview:self.cycleView];
    [self.view addSubview:self.diseaseView];
    
    
    //约束
    [self.weatherView makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.leading.trailing.equalTo(self.view);
        make.height.equalTo(80);
    }];
    
    [self.cycleView makeConstraints:^(MASConstraintMaker *make) {
        
        make.leading.trailing.equalTo(self.weatherView);
        make.top.equalTo(self.weatherView.bottom);
        make.height.equalTo(200);
    }];
    
    
    [self.diseaseView makeConstraints:^(MASConstraintMaker *make) {
        
        make.leading.trailing.equalTo(self.cycleView);
        make.top.equalTo(self.cycleView.bottom);
        make.bottom.equalTo(self.view);
    }];
    
    
    //调用设置天气的方法
    [self setWeatherData];
}

#pragma mark -
#pragma mark 给天气View设置数据
-(void)setWeatherData{
    [HZWeatherViewModel loadData:^(HZWeatherModel *weatherModel, HZWeatherSingleDayModel *singleDayModel,NSString *date) {
        //设置天气的图片
        [self.weatherView.iconImageView sd_setImageWithURL:[NSURL URLWithString:singleDayModel.nightPictureUrl]];
        
        
        
        //设置天气
        self.weatherView.weatherLabel.text = singleDayModel.weather;
        
        //设置城市
        //加下划线
        NSMutableAttributedString *content = [[NSMutableAttributedString alloc] initWithString:weatherModel.currentCity];
        
        [content addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInteger:NSUnderlineStyleSingle] range:NSMakeRange(0, [content length])];
        
       
        self.weatherView.cityLabel.attributedText = content;
        
        //设置温度
        self.weatherView.temperatureLabel.text = [NSString stringWithFormat:@"温度:%@",singleDayModel.temperature];
        
        //设置日期
        self.weatherView.dateLabel.text = [NSString stringWithFormat:@"日期:%@",date];
        
    }];
}



#pragma mark -
#pragma mark     设置弹窗侧边抽屉的按钮
-(void)setupDrawer{
    
    //创建按钮
    MMDrawerBarButtonItem* leftItem = [[MMDrawerBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"l"] style:UIBarButtonItemStylePlain target:self action:@selector(didLeft)];
    
    //添加
    self.navigationItem.leftBarButtonItem = leftItem;
    
    
}

#pragma mark -
#pragma mark     实现点击按钮的抽屉效果
-(void)didLeft{
    
    
    //实现按钮的抽屉效果
    [self.mm_drawerController toggleDrawerSide:MMDrawerSideLeft animated:YES completion:nil];
}




#pragma mark -
#pragma mark     懒加载子控件
-(HZWeathrView *)weatherView{
    if (!_weatherView) {
        
        _weatherView = [[HZWeathrView alloc]init];
    }
    return _weatherView;
}

-(HZCycleView *)cycleView{
    if (!_cycleView) {
        
        _cycleView = [[HZCycleView alloc]init];
    }
    return _cycleView;
}

-(HZDiseaseCollView *)diseaseView{
    if (!_diseaseView) {
        
        UICollectionViewFlowLayout* flow = [[UICollectionViewFlowLayout alloc]init];
        
        _diseaseView = [[HZDiseaseCollView alloc]initWithFrame:CGRectZero collectionViewLayout:flow];
    }
    return _diseaseView;
}

















@end
